﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using UserModels;
using UserRepositorys.Intface;

namespace UserRepositorys.Impl
{
    public class RoleRepository : Repository<RoleInfoModel>, IRoleRepository
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        public RoleRepository(ISqlSugarClient db) : base(db)
        {
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="roleInfo"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> AddRole(RoleInfoModel roleInfo)
        {
            return await base.Addasync(roleInfo);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> DeleteRole(int roleId)
        {
            return await base.Deleteasync(roleId);
        }
        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public  async Task<(int totalCount, int pageCount, List<RoleInfoModel> roleList)> GetRoleList(string? roleName, int pageIndex, int pageSize)
        {
            int totalCount;
            int pageCount;
            var list =from a in await base.GetAllasync()
                      where(string.IsNullOrEmpty(roleName)||a.RoleName.Contains(roleName))
                      select a;
            totalCount = list.Count();
            pageCount = (int)Math.Ceiling((totalCount*1.0)/pageSize);
            list=list.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return (totalCount, pageCount, list.ToList());
        }

        /// <summary>
        /// 根据编号查询角色
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<RoleInfoModel> GetRoleById(int roleId)
        {
            var list= await base.GetAllAsync(x => x.RoleId == roleId);
            return list.FirstOrDefault();
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="enity"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Updateasync(RoleInfoModel enity)
        {
            return await base.Updateasync(enity);
        }

        /// <summary>
        /// 所有角色
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<RoleInfoModel>> GetRoleAllList()
        {
            return await base.GetAllasync();
        }

        /// <summary>
        /// 全部角色
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<(int totalCount, int pageCount, List<RoleInfoModel> roleList)> GetRoleShow(string? roleName, int pageIndex, int pageSize)
        {
           var rolelist= await base.GetAllasync();
            var list= from a in rolelist
                       where (string.IsNullOrEmpty(roleName) || a.RoleName.Contains(roleName))
                       select a;
            int totalCount = list.Count();
            int pageCount = (int)Math.Ceiling((totalCount * 1.0) / pageSize);
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return (totalCount, pageCount, list.ToList());
        }
    }
}
